home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Pascal / Applications / PCalendar 1.0 / CalendarMain.p < prev    next >
Encoding:
Text File  |  1994-07-29  |  10.7 KB  |  373 lines  |  [TEXT/PJMM]

  1. program PCalendar;
  2.  
  3. {23 july 1994, I found Ken Longs submission of NewCalendar in alt.sources.mac. The program}
  4. {was hard-coded for displaying the 1994 calendar, and used custom routines for calculating the}
  5. {dates. I was in playing mood, I guess, so I re-used parts of NewCalendar to make a similar program,}
  6. {producing the same output, but asking for what year to make a calendar for, and places the result}
  7. {in the clipboard (so you can paste it in a program that can print it). It now uses toolbox calls, which}
  8. {means that it should work until the year 2040. I have tested it on 1993 to 1996.}
  9.  
  10.  
  11. {The following is from NewCalendar:}
  12.  
  13. {• --------------------------------------------------------------- •//}
  14. {• Feb 11, 1994                                                          •//}
  15. {• Kenneth A. Long                                                       •//}
  16. {•    Added rectangles around months drawn in test.                          •//}
  17. {• Added drawing of last 3 months which original test did not do.          •//}
  18. {• Made test draw current year.                                          •//}
  19. {• Added comments.                                                          •//}
  20. {• Removed calendar library, which was built with Calendar.c and .h}
  21. {• so the source could be traced.}
  22. {• --------------------------------------------------------------- •//}
  23.  
  24. {• --------------------------------------------------------------- •//}
  25. {• Calendar test main.c                                                  •//}
  26. {• Simple and quick test of caldendar routines                              •//}
  27. {• --------------------------------------------------------------- •//}
  28.  
  29.  
  30.     uses
  31.         Calendar;
  32.  
  33. {Resource IDs}
  34.     const
  35.         kWeekdayStrings = 128;
  36.         kMonthStrings = 129;
  37.         kCalendarWindowId = 128;
  38.         kYearDlogID = 128;
  39.         kDaysStringId = 128;
  40.  
  41. {Constants for size of the boxes}
  42.     const
  43.         kRectHeight = 104;
  44.         kRectWidth = 124;
  45.         kHSpacing = 2;
  46.         kVSpacing = 10;
  47.         kFirstHOffset = 3;
  48.         kFirstVOffset = 23; {7}
  49.  
  50. {Globals}
  51.     var
  52.         gMonthDays: array[1..12] of integer;
  53.  
  54. {Strings read from resources:}
  55.         gpDaysOfWeek: array[1..7] of Str255;
  56.         gpMonthsOfYear: array[1..12] of Str255;
  57.         gDaysString: Str255;
  58.  
  59. {• --------------------------------------------------------------- •//}
  60. {• CenterWindow()                                                          •//}
  61. {• --------------------------------------------------------------- •//}
  62.  
  63.     procedure CenterWindow (theDialog: WindowPtr);
  64.         var
  65.             centerPos: Point;
  66.             dialogRect: Rect;
  67.             desktopExtent: Rect;
  68.     begin
  69.         desktopExtent := GetGrayRgn^^.rgnBBox;
  70.  
  71.     {• get dialog's rectangle •//}
  72.         dialogRect := theDialog^.portRect;
  73.  
  74.     {• calculate screen center position •//}
  75.         with desktopExtent do
  76.             SetPt(centerPos, (right + left) div 2, (bottom - top) div 3 + top);
  77.  
  78.     {• center dialog's rectangle •//}
  79.         MoveWindow(theDialog, centerPos.h - (dialogRect.right - dialogRect.left) div 2, centerPos.v - (dialogRect.bottom - dialogRect.top) div 3, FALSE);
  80.  
  81.     end; {• CenterDialog() •//}
  82.  
  83. {• --------------------------------------------------------------- •//}
  84. {• SetDefaultText()                                                      •//}
  85. {• --------------------------------------------------------------- •//}
  86.  
  87.     procedure SetDefaultText (fn: integer; fc: Style; sz: integer);
  88.     begin
  89.         TextFont(fn);
  90.         TextFace(fc);
  91.         TextSize(sz);
  92.     end;
  93.  
  94. {• --------------------------------------------------------------- •//}
  95. {• DrawMonth()                                                        •//}
  96. {• DrawMonth() will draw theMonth or theYear at location start in    •//}
  97. {• in thePort.                                                        •//}
  98. {• --------------------------------------------------------------- •//}
  99.  
  100.     procedure DrawMonth (thePort: GrafPtr; start: Point; theMonth: Integer; theYear: Integer);
  101.         var
  102.             oldPort: GrafPtr;
  103.             fInfo: FontInfo;
  104.             rowSize, currRow, ddayOfMonth, numDays, dayIndex, horizCenter, mWidth: Integer;
  105.             pos: Point;
  106.             tmpstr: Str255;
  107.             tmp: integer;
  108.  
  109.         procedure DrawDay (dn, dw: integer);
  110.         begin
  111.             MoveTo(pos.h + (dw - 1) * StringWidth('   '), pos.v + rowSize * currRow);
  112.             NumToString(dn, tmpstr);
  113.             if ord(tmpstr[0]) = 1 then
  114.                 DrawString(' ');
  115.             DrawString(tmpstr);
  116.         end; {DrawDay}
  117.  
  118.     begin
  119.         currRow := 1;
  120.  
  121.     {• DrawDay() - 'Neat' (in the clean sense) macro to draw day }
  122.     {• number under the correct day of week.}
  123.  
  124.         if ((theMonth <= 0) or (theMonth > 12)) then
  125.             begin
  126.                 exit(DrawMonth);
  127.             end;
  128.         if (theYear <= 0) then
  129.             begin
  130.                 exit(DrawMonth);
  131.             end;
  132.         if (thePort = nil) then
  133.             begin
  134.                 exit(DrawMonth);
  135.             end;
  136.  
  137.         GetPort(oldPort);
  138.         SetPort(thePort);
  139.  
  140.         GetFontInfo(fInfo);
  141.  
  142.     {• Initalize starting position.}
  143.         pos := start;
  144.  
  145.     {• Get size of row.}
  146.         rowSize := fInfo.ascent + fInfo.descent + fInfo.leading;
  147.  
  148.         horizCenter := (kRectWidth + kHSpacing) div 2; {• Calculate center position.}
  149.  
  150.     {• Get half size of month name.}
  151.         mWidth := StringWidth(gpMonthsOfYear[theMonth]) div 2;
  152.  
  153.     {• draw month name - centered •//}
  154.  
  155.         MoveTo(pos.h + (horizCenter - mWidth), pos.v + (rowSize * currRow));
  156.         DrawString(gpMonthsOfYear[theMonth]);
  157.  
  158.     {• move down one row •//}
  159.         currRow := currRow + 1;
  160.  
  161. {Fine tuning (should have been automatic, really)}
  162.         pos.h := pos.h + 2;
  163.         pos.v := pos.v + 3; {• plus a little more •//}
  164.  
  165.     {• draw the days header •//}
  166.  
  167.         MoveTo(pos.h, pos.v + (rowSize * currRow));
  168.         DrawString(gDaysString);
  169.  
  170.         currRow := currRow + 1; {• move down one row •//}
  171.  
  172.     {• Draw the line separator •//}
  173.         MoveTo(pos.h - 2, pos.v + (rowSize * currRow) - (rowSize div 2));
  174.         PenSize((thePort^.txSize div 9), (thePort^.txSize div 9));
  175.         Line((kRectWidth) - 1, 0);
  176.  
  177.     {• move down one row •//}
  178.         currRow := currRow + 1;
  179.  
  180.     {• Draw the first day of the month}
  181.         tmp := DayOfMonth(1, theMonth, theYear);
  182.         DrawDay(1, tmp);
  183.  
  184.     {• Get the number of days in month the particular month.}
  185.     {• Assign that value to numDays.}
  186.         numDays := DaysInMonth(theMonth, theYear);
  187.  
  188.     {• Cycle through day 2 to the last day, drawing each day.}
  189.         for dayIndex := 2 to numDays do
  190.             begin
  191.         {• get day of week Sunday to Saturday.}
  192.                 ddayOfMonth := DayOfMonth(dayIndex, theMonth, theYear);
  193.  
  194.         {• if Sunday, move to next line •//}
  195.  
  196.                 if (ddayOfMonth = SUNDAY) then
  197.                     begin
  198.                         currRow := currRow + 1;
  199.                     end;
  200.         {• draw the day at the right location •//}
  201.                 DrawDay(dayIndex, ddayOfMonth);
  202.  
  203.             end; {• for •//}
  204.  
  205.         SetPort(oldPort);
  206.     end; {• DrawMonth() •//}
  207.  
  208. {The following must be globals since the main routine needs them}
  209.     var
  210.         janRect, febRect, marRect, aprRect, mayRect, junRect, julRect, augRect, sepRect, octRect, novRect, decRect: Rect;
  211.  
  212.     procedure Rectify;
  213.         function MakeOffsetRect (r: Rect; hOffs, vOffs: integer): Rect;
  214.         begin
  215.             OffsetRect(r, hOffs, vOffs);
  216.             MakeOffsetRect := r;
  217.         end; {MakeOffsetRect}
  218.     begin
  219.         SetRect(JanRect, 0, 0, kRectWidth, kRectHeight);        {• 104 by 124.}
  220.         OffsetRect(janRect, kFirstHOffset, kFirstVOffset);
  221.         febRect := MakeOffsetRect(janRect, kRectWidth + kHSpacing, 0);
  222.         marRect := MakeOffsetRect(febRect, kRectWidth + kHSpacing, 0);
  223.         aprRect := MakeOffsetRect(marRect, kRectWidth + kHSpacing, 0);
  224.  
  225.         mayRect := MakeOffsetRect(janRect, 0, kRectHeight + kVSpacing);
  226.  
  227.         junRect := MakeOffsetRect(mayRect, kRectWidth + kHSpacing, 0);
  228.         julRect := MakeOffsetRect(junRect, kRectWidth + kHSpacing, 0);
  229.         augRect := MakeOffsetRect(julRect, kRectWidth + kHSpacing, 0);
  230.  
  231.         sepRect := MakeOffsetRect(mayRect, 0, kRectHeight + kVSpacing);
  232.  
  233.         octRect := MakeOffsetRect(sepRect, kRectWidth + kHSpacing, 0);
  234.         novRect := MakeOffsetRect(octRect, kRectWidth + kHSpacing, 0);
  235.         decRect := MakeOffsetRect(novRect, kRectWidth + kHSpacing, 0);
  236.  
  237.         FrameRect(janRect);
  238.         FrameRect(febRect);
  239.         FrameRect(marRect);
  240.         FrameRect(aprRect);
  241.         FrameRect(mayRect);
  242.         FrameRect(junRect);
  243.         FrameRect(julRect);
  244.         FrameRect(augRect);
  245.         FrameRect(sepRect);
  246.         FrameRect(octRect);
  247.         FrameRect(novRect);
  248.         FrameRect(decRect);
  249.     end; {Rectify}
  250.  
  251.     procedure InitArrays (year: integer);
  252.         var
  253.             i: integer;
  254.     begin
  255.         for i := 1 to 12 do
  256.             begin
  257.                 gMonthDays[i] := DaysInMonth(i, year);
  258.             end;
  259.  
  260.         for i := 1 to 7 do
  261.             GetIndString(gpDaysOfWeek[i], kWeekdayStrings, i);
  262.         for i := 1 to 12 do
  263.             GetIndString(gpMonthsOfYear[i], kMonthStrings, i);
  264.  
  265.         gDaysString := GetString(kDaysStringId)^^;
  266.     end;
  267.  
  268.     function AskYear: integer;
  269.         var
  270.             dialog: DialogPtr;
  271.             oldPort: GrafPtr;
  272.             itemHit: integer;
  273.             str: str255;
  274.  
  275.             kind: integer;
  276.             item: ControlHandle;
  277.             box: Rect;
  278.             year: Longint;
  279.     begin
  280.         GetPort(oldPort);
  281.         dialog := GetNewDialog(kYearDlogID, nil, WindowPtr(-1));
  282.         SetPort(dialog);
  283.  
  284.         SelIText(dialog, 3, 0, 32767);
  285.         itemHit := -1;
  286.         while itemHit <> 1 do
  287.             ModalDialog(nil, itemHit);
  288.         GetDItem(dialog, 3, kind, Handle(item), box);
  289.         GetIText(handle(item), str);
  290.         StringToNum(str, year);
  291.         AskYear := year;
  292.         DisposeDialog(dialog);
  293.         SetPort(oldPort);
  294.     end;
  295.  
  296. {• --------------------------------------------------------------- •//}
  297. {• main()                                                                  •//}
  298. {• Main test procedure.                                                  •//}
  299. {• --------------------------------------------------------------- •//}
  300.  
  301. {procedure main;}
  302.     var
  303.         monthWindow: WindowPtr;
  304.         f: FontInfo;
  305.         pic: PicHandle;
  306.         ignore: integer;
  307.         theYear: integer;
  308.         title: Str255;
  309. begin
  310. {Toolbox init is automatic.}
  311.  
  312.     theYear := AskYear;
  313.  
  314.     InitArrays(theYear); {Year should be variable!}
  315.  
  316.     monthWindow := GetNewWindow(kCalendarWindowId, nil, WindowPtr(-1));
  317.  
  318.     if monthWindow <> nil then
  319.         begin
  320.             CenterWindow(monthWindow);
  321.             SetPort(monthWindow);
  322.             ShowWindow(monthWindow);
  323.  
  324.             NumToString(theYear, title);
  325.  
  326. {Draw into a picture.}
  327.             pic := OpenPicture(monthWindow^.portRect);
  328.  
  329.             SetDefaultText(monaco, [bold], 12);    {• Monaco fits all in}
  330.             GetFontInfo(f);                    {• a 512x384 screen.}
  331.             MoveTo(monthWindow^.portRect.right div 2 - StringWidth(title) div 2, f.ascent + f.descent + f.leading);
  332.             DrawString(title);
  333.  
  334.             SetDefaultText(monaco, [], 9);    {• Monaco fits all in}
  335.             GetFontInfo(f);                    {• a 512x384 screen.}
  336.  
  337.             Rectify;
  338.             DrawMonth(monthWindow, janRect.topLeft, JANUARY, theYear);
  339.             DrawMonth(monthWindow, febRect.topLeft, FEBRUARY, theYear);
  340.             DrawMonth(monthWindow, marRect.topLeft, MARCH, theYear);
  341.             DrawMonth(monthWindow, aprRect.topLeft, APRIL, theYear);
  342.             DrawMonth(monthWindow, mayRect.topLeft, MAY, theYear);
  343.             DrawMonth(monthWindow, junRect.topLeft, JUNE, theYear);
  344.             DrawMonth(monthWindow, julRect.topLeft, JULY, theYear);
  345.             DrawMonth(monthWindow, augRect.topLeft, AUGUST, theYear);
  346.             DrawMonth(monthWindow, sepRect.topLeft, SEPTEMBER, theYear);
  347.             DrawMonth(monthWindow, octRect.topLeft, OCTOBER, theYear);
  348.             DrawMonth(monthWindow, novRect.topLeft, NOVEMBER, theYear);
  349.             DrawMonth(monthWindow, decRect.topLeft, DECEMBER, theYear);
  350.             ClosePicture;
  351.  
  352.             DrawPicture(pic, pic^^.picFrame);
  353.  
  354.             ignore := LoadScrap;
  355.             ignore := ZeroScrap;
  356.             HLock(Handle(pic));
  357.             ignore := PutScrap(GetHandleSize(Handle(pic)), 'PICT', pointer(Handle(pic)^));
  358.             if SystemEdit(3) then
  359.                 ;    { so multifinder will take the scrap }
  360.             HUnLock(Handle(pic));
  361.             ignore := UnLoadScrap;
  362.  
  363.             FlushEvents(everyEvent, 0);
  364.  
  365.             while not Button do            {• Do it until THE Button is hit.}
  366.                 ;
  367.  
  368.             FlushEvents(everyEvent, 0);    {• Then act like nothing happened.}
  369.             DisposeWindow(monthWindow);    {• And then dump everything}
  370.         end;
  371. end.        {• "Get back home, Loretta!"}
  372.  
  373. {•*************************************************************************************************•//}